const useColor = (_color: string) => {
  if (!_color) return
  let newColor = parseColor(_color)
  if (!newColor) return console.error('自定义颜色格式错误，请检查')
  // rgb: 'rgb(64,255,218)'
  let rgb =
    'rgb(' +
    (newColor.r * 1.47 > 255 ? 255 : newColor.r * 1.47) +
    ',' +
    (newColor.g * 1.41 > 255 ? 255 : newColor.g * 1.41) +
    ',' +
    (newColor.b * 1.02 > 255 ? 255 : newColor.b * 1.02) +
    ')'
  return Object.assign(newColor, newColor, {
    newrgb: rgb
  })
}
const parseColor = (val: string) => {
  // 16进制颜色值的正则
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  // 把颜色值变成小写
  var color = val.toLowerCase()
  var result = ''
  let rgb
  if (reg.test(color)) {
    // 如果只有三位的值，需变成六位，如：#fff => #ffffff
    if (color.length === 4) {
      var colorNew = '#'
      for (var i = 1; i < 4; i += 1) {
        colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1))
      }
      color = colorNew
    }
    // 处理六位的颜色值，转为RGB
    var colorChange: Array<number> = []
    for (var i = 1; i < 7; i += 2) {
      colorChange.push(parseInt('0x' + color.slice(i, i + 2)))
    }
    result = 'rgb(' + colorChange.join(',') + ')'
    rgb = { rgb: result, r: colorChange[0], g: colorChange[1], b: colorChange[2] }
  } else {
    result = '无效'
    rgb = undefined
  }
  return rgb
}
export default useColor
